<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

$api = app('Dingo\Api\Routing\Router');

$api->version('v1',  [
    'namespace' => 'App\Http\Controllers\Api',
    'middleware' => ['serializer:array', 'bindings', 'cors']
],function($api) {
    $api->group([
       'middleware' => 'api.throttle',
       'limit' => config('api.rate_limits.sign.limit'),
       'expires' => config('api.rate_limits.sign.expires'),
    ], function($api) {
        // 短信验证码
        /*
        param：name:15815111389
        {
		    "status": true,
		    "data": {
		        "successflag": true,
		        "key": "verificationCode_RkKS1udeMdsmLyB",
		        "expired_at": "2018-08-24 15:04:06"
		    },
		    "message": "发送短信成功"
		}
		*/
        $api->post('verificationCodes', 'VerificationCodesController@store')
            ->name('api.verificationCodes.store');


        // 用户短信登录
        /*
        param：verification_key:verificationCode_84HsksFZbcD3z9A
				verification_code:561172

        {
		    "status": true,
		    "data": {
		        "successflag": true,
		        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leC50ZXN0XC9hcGlcL3VzZXJzIiwiaWF0IjoxNTM1MDkzOTUxLCJleHAiOjE1MzUwOTc1NTEsIm5iZiI6MTUzNTA5Mzk1MSwianRpIjoiY0F0QWlQNk5DWlN6WEo3ciIsInN1YiI6MTEsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.I8bRZuNhswOHSWETYGFDNX3OzPA81HxwprAD3PS4f08",
		        "expires_in": 3600
		    },
		    "message": "成功"
		}
		*/
        $api->post('users', 'UsersController@store')
            ->name('api.users.login');

        // admin登录
         /*
        param：name:admin
				password:admin123
        {
		    "status": true,
		    "data": {
		        "successflag": true,
		        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leC50ZXN0XC9hcGlcL2F1dGhvcml6YXRpb25zIiwiaWF0IjoxNTM1MDk1MTUyLCJleHAiOjE1MzUwOTg3NTIsIm5iZiI6MTUzNTA5NTE1MiwianRpIjoiWnk2M0tWWEFvZ3BzTFJwQSIsInN1YiI6MSwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyJ9.KnReWqPi0DJySRARMbYGr_tetc0E6i-VDJNdyMbGLGw",
		        "expires_in": 3600
		    },
		    "message": "登录成功"
		}
		*/
		$api->post('authorizations', 'AuthorizationsController@store')
            ->name('api.authorizations.store');
        // 刷新token
        /*
        param：token

        {
		    "status": true,
		    "data": {
		        "successflag": true,
		        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9leC50ZXN0XC9hcGlcL2F1dGhvcml6YXRpb25zXC9jdXJyZW50IiwiaWF0IjoxNTM1MDkzOTUxLCJleHAiOjE1MzUwOTc4MzMsIm5iZiI6MTUzNTA5NDIzMywianRpIjoiUDlzaEtEYkpVOTlqeWFhbCIsInN1YiI6MTEsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.rrqm9mWQUSBOl0kvDPbqgI_7iOjUCwcK0OfdeG-XQTA",
		        "expires_in": 3600
		    },
		    "message": "登录成功"
		}
        */
        $api->put('authorizations/current', 'AuthorizationsController@update')
            ->name('api.authorizations.update');

        // 删除token
        /*
        param：token
        {
		    "status": true,
		    "data": {
		        "successflag": true
		    },
		    "message": "退出登录成功"
		}
		*/

        $api->delete('authorizations/current', 'AuthorizationsController@destroy')
            ->name('api.authorizations.destroy');

        //区域列表 http://{{host}}/api/areas
        $api->get('areas', 'AreasController@index')
            ->name('api.areas.index');
        //区域搜手 http://{{host}}/api/areas/search?name='本市'
        $api->get('areas/search', 'AreasController@search')
            ->name('api.areas.search');

        //区域详情
        $api->get('areas/{area}', 'AreasController@show')
            ->name('api.areas.show');

        //商家分类列表 http://{{host}}/api/categories
        $api->get('categories', 'CategoriesController@index')
            ->name('api.categories.index');
        //区域搜手 http://{{host}}/api/categories/search?name='旅游'
        $api->get('categories/search', 'CategoriesController@search')
            ->name('api.categories.search');

        //商家分类详情
        $api->get('categories/{category}', 'CategoriesController@show')
            ->name('api.categories.show');

        //商品分类列表 http://{{host}}/api/types
        $api->get('types', 'TypesController@index')
            ->name('api.types.index');
        //区域搜手 http://{{host}}/api/types/search?name='旅游'
        $api->get('types/search', 'TypesController@search')
            ->name('api.types.search');

         //商品分类详情
        $api->get('types/{type}', 'TypesController@show')
            ->name('api.types.show');

        //商家列表 http://{{host}}/api/companies?include=users,products,categories
        $api->get('companies', 'CompaniesController@index')
            ->name('api.companies.index');
        //商家搜索列表 http://{{host}}/api/companies/search?name=传媒有限公司&&cate_id=3&&include=users,products,categories
        $api->get('companies/search', 'CompaniesController@search')
            ->name('api.companies.search');
        //商家详情 http://{{host}}/api/companies/{:id}?include=users,products,categories
        $api->get('companies/{company}', 'CompaniesController@show')
            ->name('api.companies.show');
        //商家分店列表 http://{{host}}/api/filiales?include=company
        $api->get('filiales', 'FilialesController@index')
            ->name('api.filiales.index');
        //商家分店搜索列表 http://{{host}}/api/filiales/search?name=传媒有限分公司&&phone=110&&address=光华路&&company_id=1&&include=company
        $api->get('filiales/search', 'FilialesController@search')
            ->name('api.filiales.search');
        //某商家分店列表 http://{{host}}/api/companies/{:id}/filiales?include=company
        $api->get('companies/{company}/filiales', 'FilialesController@companyIndex')
            ->name('api.filiales.companyIndex');
        //商家分店详情 http://{{host}}/api/companies/filiales?include=company
        $api->get('companies/{company}/filiales/{filiale}', 'FilialesController@show')
            ->name('api.filiales.show');

        //产品列表 http://{{host}}/api/products?include=company
        $api->get('products', 'ProductsController@index')
            ->name('api.products.index');
         //产品搜索列表 http://{{host}}/api/products/search?name=corporis&&type_id=1&&price=50&&include=company
        $api->get('products/search', 'ProductsController@search')
            ->name('api.products.search');
        //某商家产品列表 http://{{host}}/api/companies/{:id}/products?include=company
        $api->get('companies/{company}/products', 'ProductsController@companyIndex')
            ->name('api.products.companyIndex');
        //商家分店详情 http://{{host}}/api/companies/{company}/products/{product}?include=company
        $api->get('companies/{company}/products/{product}', 'ProductsController@show')
            ->name('api.products.show');

        //奖品列表 http://{{host}}/api/prizes?include=product
        $api->get('prizes', 'PrizesController@index')
            ->name('api.prizes.index');
        //奖品搜索列表 http://{{host}}/api/prizes/search?product_id=1&&status=1&&gtremain_quantity=100&&ltredeeming_quantity=99&&gtredeemed_quantity=200&&accumulate_point=300&&shelves_begin=2018-09-18&&shelves_end=2018-09-28&&&&redeeming_begin=2018-09-18&&redeeming_end=2018-09-28&&redeemed_begin=2018-09-18&&redeemed_end=2018-09-28include=product
        $api->get('prizes/search', 'PrizesController@search')
            ->name('api.prizes.search');
        //某商家奖品列表 http://{{host}}/api/companies/{company}/prizes?include=product
        $api->get('companies/{company}/prizes', 'PrizesController@companyIndex')
            ->name('api.prizes.companyIndex');
        //商家分店详情 http://{{host}}/api/products/{product}/prizes/{prize}?include=product
        $api->get('products/{product}/prizes/{prize}', 'PrizesController@show')
            ->name('api.prizes.show');



        // 需要 token 验证的接口
        $api->group(['middleware' => 'api.auth'], function($api) {
            //上传文件 param:file
            // {
            //     "status": true,
            //     "data": "http://ex.test/uploads/images/ex/201808/24/10_1535096200_CGqtUTwqLm.png",
            //     "message": "上传文件成功"
            // }
            $api->post('upload', 'UploadController@upload')
                ->name('api.upload');

            // 当前登录用户信息 param：token http://{{host}}/api/user
            // {
            //     "id": 10,
            //     "name": "17054147184",
            //     "nickname": "admin",
            //     "realname": "admin",
            //     "phone": "15815111389",
            //     "address": "广东省汕头市潮南区第36街道第493号",
            //     "avatar": "https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/LOnMrqbHJn.png?imageView2/1/w/200/h/200",
            //     "introduction": "Assumenda sed quo iure quisquam.",
            //     "created_at": "2018-08-23 14:34:30",
            //     "updated_at": "2018-08-23 16:26:20"
            // }
            $api->get('user', 'UsersController@me')
                ->name('api.user.show');

            //管理员关联的商家列表 http://{{host}}/api/user/companies?include=users,categories
            $api->get('user/companies', 'CompaniesController@userIndex')
                ->name('api.user.companies');

            // 编辑登录用户信息
            $api->patch('user', 'UsersController@update')
                ->name('api.user.update');
             // 指定角色 param:role 'admin'
            $api->post('users/{user}/assignRole', 'RolesController@assignRole')
                ->name('api.roles.assignRole');

            // 区域
            $api->post('areas', 'AreasController@store')
                ->name('api.areas.store');

            $api->patch('areas/{area}', 'AreasController@update')
                ->name('api.areas.update');

            $api->delete('areas/{area}', 'AreasController@destroy')
                ->name('api.areas.destroy');

            // 商家类别
            $api->post('categories', 'CategoriesController@store')
                ->name('api.categories.store');

            $api->patch('categories/{category}', 'CategoriesController@update')
                ->name('api.categories.update');

            $api->delete('categories/{category}', 'CategoriesController@destroy')
                ->name('api.categories.destroy');

            // 商品类别
            $api->post('types', 'TypesController@store')
                ->name('api.types.store');

            $api->patch('types/{type}', 'TypesController@update')
                ->name('api.types.update');

            $api->delete('types/{type}', 'TypesController@destroy')
                ->name('api.types.destroy');

            // 商家
            $api->post('companies', 'CompaniesController@store')
                ->name('api.companies.store');

            $api->patch('companies/{company}', 'CompaniesController@update')
                ->name('api.companies.update');

            $api->delete('companies/{company}', 'CompaniesController@destroy')
                ->name('api.companies.destroy');

            //添加管理员 param：user_id
            $api->post('companies/{company}/users', 'CompaniesController@addAdmin')
                ->name('api.companies.addAdmin');

            //添加分类   param：cate_ids 可以是数组
            $api->post('companies/{company}/categories', 'CompaniesController@addCategory')
                ->name('api.companies.addCategory');

            // 分店
            $api->post('companies/{company}/filiales', 'FilialesController@store')
                ->name('api.filiales.store');

            $api->patch('companies/{company}/filiales/{filiale}', 'FilialesController@update')
                ->name('api.filiales.update');

            $api->delete('companies/{company}/filiales/{filiale}', 'FilialesController@destroy')
                ->name('api.filiales.destroy');
            //商家分店关联   param：company_id
            $api->post('filiales/{filiale}', 'FilialesController@attachCompany')
                ->name('api.filiales.attachCompany');

            // 产品
            $api->post('companies/{company}/products', 'ProductsController@store')
                ->name('api.products.store');

            $api->patch('companies/{company}/products/{product}', 'ProductsController@update')
                ->name('api.products.update');

            $api->delete('companies/{company}/products/{product}', 'ProductsController@destroy')
                ->name('api.products.destroy');
            //商家产品关联   param：company_id
            $api->post('products/{product}', 'ProductsController@attachCompany')
                ->name('api.products.attachCompany');

            //添加产品类型关联   param：type_ids
            $api->post('products/{product}/types', 'CompaniesController@addTypes')
                ->name('api.companies.addTypes');

            // 奖品
            $api->post('products/{product}/prizes', 'PrizesController@store')
                ->name('api.prizes.store');

            $api->patch('products/{product}/prizes/{prize}', 'PrizesController@update')
                ->name('api.prizes.update');

            $api->delete('products/{product}/prizes/{prize}', 'PrizesController@destroy')
                ->name('api.prizes.destroy');

            //添加奖品产品关联   param：product_id
            $api->post('prizes/{prize}', 'PrizesController@attachProduct')
                ->name('api.prizes.attachProduct');

            // 兑奖纪录列表 http://{{host}}/api/prizeRecords?include=prize,user
            $api->get('prizeRecords', 'PrizeRecordsController@index')
                ->name('api.prizeRecords.index');
            // 兑奖纪录搜索列表 http://{{host}}/api/prizeRecords/search?prize_id=1&&user_id=1&&filiale_id=10&&redeem_begin=2018-09-08&&redeem_end=2018-09-18&&include=prize,user
            $api->get('prizeRecords/search', 'PrizeRecordsController@search')
                ->name('api.prizeRecords.search');
            //用户兑奖纪录列表 http://{{host}}/api/user/prizeRecords?include=prize,user
            $api->get('user/prizeRecords', 'PrizeRecordsController@userIndex')
                ->name('api.prizeRecords.userIndex');
            //兑奖纪录详情 http://{{host}}/api/prizeRecords/{prizeRecord}?include==prize,user,filiale
            $api->get('prizes/{prize}/prizeRecords/{prizeRecord}', 'PrizeRecordsController@show')
                ->name('api.prizeRecords.show');
            //用户兑奖
            $api->post('prizes/{prize}/prizeRecords', 'PrizeRecordsController@store')
                ->name('api.prizeRecords.store');

            $api->patch('prizes/{prize}/prizeRecords/{prizeRecord}', 'PrizeRecordsController@update')
                ->name('api.prizeRecords.update');

            $api->delete('prizes/{prize}/prizeRecords/{prizeRecord}', 'PrizeRecordsController@destroy')
                ->name('api.prizeRecords.destroy');
            //管理员领奖 param：user_id,redeem_code,filiale_id,remark(可选)
            $api->post('prizes/{prize}/prizeRecords/{prizeRecord}/adminRedeemed', 'PrizeRecordsController@adminRedeemed')
                ->name('api.prizeRecords.adminRedeemed');
            //用户领奖 param：company_userid,redeem_code,filiale_id,remark(可选)
            $api->post('prizes/{prize}/prizeRecords/{prizeRecord}/userRedeemed', 'PrizeRecordsController@userRedeemed')
                ->name('api.prizeRecords.userRedeemed');
            //用户兑奖
            $api->post('prizes/{prize}/prizeRecords}/userRedeemeing', 'PrizeRecordsController@userRedeemeing')
                ->name('api.prizeRecords.userRedeemeing');

            // 兑奖日志列表 http://{{host}}/api/redeemLogs?include=prize,user
            $api->get('redeemLogs', 'RedeemLogsController@index')
                ->name('api.redeemLogs.index');
            // 兑奖日志搜索列表 http://{{host}}/api/redeemLogs/search?prize_id=1&&user_id=1&&filiale_id=10&&company_userid=1&&redeem_begin=2018-09-08&&redeem_end=2018-09-18&&include=prize,user
            $api->get('redeemLogs/search', 'RedeemLogsController@search')
                ->name('api.redeemLogs.search');
            //用户兑奖日志列表 http://{{host}}/api/user/redeemLogs?include=prize,user
            $api->get('user/redeemLogs', 'RedeemLogsController@userIndex')
                ->name('api.redeemLogs.userIndex');
            //管理员兑奖日志列表 http://{{host}}/api/user/redeemLogs?include=prize,user
            $api->get('manager/redeemLogs', 'RedeemLogsController@managerIndex')
                ->name('api.redeemLogs.managerIndex');
            //兑奖日志详情 http://{{host}}/api/redeemLogs/{redeemLog}?include==prize,user,filiale
            $api->get('redeemLogs/{redeemLog}', 'RedeemLogsController@show')
                ->name('api.redeemLogs.show');
            });

            //权限列表 http://{{host}}/api/permissions
            $api->get('permissions', 'PermissionsController@index')
                ->name('api.permissions.index');

            $api->get('user/permissions', 'PermissionsController@userIndex')
                ->name('api.permissions.userIndex');
            //权限详情
            $api->get('permissions/{permission}', 'PermissionsController@show')
                ->name('api.permissions.show');

            $api->post('permissions', 'PermissionsController@store')
                ->name('api.permissions.store');

            $api->patch('permissions/{permission}', 'PermissionsController@update')
                ->name('api.permissions.update');

            $api->delete('permissions/{permission}', 'PermissionsController@destroy')
                ->name('api.permissions.destroy');

             //权限列表 http://{{host}}/api/roles
            $api->get('roles', 'RolesController@index')
                ->name('api.roles.index');

            $api->get('user/roles', 'RolesController@userIndex')
                ->name('api.roles.userIndex');
            //权限详情
            $api->get('roles/{roles}', 'RolesController@show')
                ->name('api.roles.show');

            $api->post('roles', 'RolesController@store')
                ->name('api.roles.store');

            $api->patch('roles/{role}', 'RolesController@update')
                ->name('api.roles.update');

            $api->delete('roles/{role}', 'RolesController@destroy')
                ->name('api.roles.destroy');
            // 添加权限组 param:permissions: ['publish articles', 'unpublish articles']
            $api->post('roles/{role}/addPermissons', 'RolesController@addPermissons')
                ->name('api.roles.addPermissons');
            // 删除权限 param:permission： 'publish articles'
            $api->post('roles/{role}/removePermissons', 'RolesController@removePermissons')
                ->name('api.roles.removePermissons');

    });

});
